// 深度克隆
export function mergeClone (items: any): any {
  const result: any = Array.isArray(items) ? [] : {}
  Object.keys(items).forEach(key => {
    if (typeof items[key] === 'object' && items[key] !== null) {
      result[key] = mergeClone(items[key])
    } else {
      result[key] = items[key]
    }
  })
  return result
}

/**
* 数组去重
* @param arr: Array<any> 需要去重的数组
* @returns result: Array<any> 去重后的数组
*/
export function unique (arr: any[]): any[] {
  const result: any[] = []
  const obj: any = {}
  arr.forEach(item => {
    if (obj[item] === undefined || obj[item] === false) {
      obj[item] = true
      result.push(item)
    }
  })
  return result
}

/**
 * 事件转发
 * @param el 父级节点
 * @param type 事件类型
 * @param fn 事件方法
 * @param selector 子级节点
 */
export function eventForward (el: HTMLElement | string, type: string, fn: any, selector?: string): void {
  if (typeof el === 'string') {
    el = document.querySelector(el) as HTMLElement
  }
  if (selector === null) {
    el.addEventListener(type, fn as EventListenerOrEventListenerObject)
  } else {
    el.addEventListener(type, function (event: Event) {
      const target = event.target as any
      if (target.matches(selector).length > 0) {
        fn.apply(target, event)
      }
    })
  }
}

/**
* 数组分页
* @param array 需要分页的数组
* @param pageNo 页码
* @param pageSize 每页显示条数
* @returns
*/
export function pagination (array: any[], pageNo = 1 as number, pageSize = 10 as number): any[] {
  try {
    const offset = (pageNo - 1) * pageSize
    return (offset + pageSize >= array.length) ? array.slice(offset, array.length) : array.slice(offset, offset + pageSize)
  } catch (e) {
    console.error(e)
    return []
  }
}
/**
 * 图片加载
 * @param url 图片地址
 * @returns Promise<any>
 */
export async function asyncImage (url: string): Promise<any> {
  let timer: any = null
  if (timer !== null) clearTimeout(timer)
  return await new Promise((resolve, reject) => {
    const image: HTMLImageElement = document.createElement('img')
    image.style.display = 'none'
    image.src = url
    image.onload = () => {
      timer = setTimeout(() => {
        resolve(null)
      }, 1000)
    }
    image.onerror = (e) => {
      reject(e)
      console.error('资源加载失败')
    }
  })
}

/**
 * 下载文件
 * @param data 文件数据
 * @param type // 文件类型
 * @param fileName 文件名称
 * @returns
 */
export async function downLoad (data: BlobPart, type = 'application/vnd.ms-excel', fileName = ''): Promise<any> {
  return await new Promise((resolve, reject) => {
    try {
      fileName += new Date().getTime()
      // 获取Blob
      const blob: Blob = new Blob([data], { type })
      // 创建临时url
      const url: string = window.URL.createObjectURL(blob)
      // 创建a标签
      const link: HTMLAnchorElement = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      // 文件名中有中文 则对文件名进行转码
      link.download = decodeURIComponent(fileName)
      link.setAttribute('download', fileName)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link) // 下载完成移除元素
      window.URL.revokeObjectURL(url) // 释放掉blob对象
      resolve(null)
    } catch (e) {
      reject(e)
    }
  })
}
